package tr.edu.metu.is.mega.client.core;

import tr.edu.metu.is.mega.client.resource.MEGAResources;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

/**
 * Toolbar with 4 buttons; insert, update, delete, select.
 */
public class MegaToolbar extends ToolBar {

    private final Button insertButton = new TooltipButton(MEGAResources.RESOURCEBUNDLE.create(),
            AbstractImagePrototype.create(MEGAResources.ICONBUNDLE.add()));

    private final Button updateButton = new TooltipButton(MEGAResources.RESOURCEBUNDLE.update(),
            AbstractImagePrototype.create(MEGAResources.ICONBUNDLE.update()));

    private final Button deleteButton = new TooltipButton(MEGAResources.RESOURCEBUNDLE.delete(),
            AbstractImagePrototype.create(MEGAResources.ICONBUNDLE.delete()));

    private final Button selectButton = new TooltipButton(MEGAResources.RESOURCEBUNDLE.select(),
            AbstractImagePrototype.create(MEGAResources.ICONBUNDLE.plugin()));

    /**
     * 
     * Default constructor.
     */
    public MegaToolbar() {

        add(insertButton);
        addNewButton(updateButton);
        addNewButton(deleteButton);

        setSpacing(5);
    }

    /**
     * Alternative constructor.
     * 
     * @param insertFlag insert button enable flag
     * @param updateFlag update button enable flag
     * @param deleteFlag delete button enable flag
     */
    public MegaToolbar(boolean insertFlag, boolean updateFlag, boolean deleteFlag) {
        if (insertFlag) {
            add(insertButton);
        }

        if (updateFlag) {
            addNewButton(updateButton);
        }

        if (deleteFlag) {
            addNewButton(deleteButton);
        }

        setSpacing(5);
    }

    /**
     * Adds new button to toolbar
     * 
     * @param newButton adds newButton to toolbar
     */
    public void addNewButton(Button newButton) {
        add(newButton);
    }

    /**
     * Changes visibility of insert button
     * 
     * @param visible set to insertButton's visible flag
     */
    public void showInsertButton(boolean visible) {
        insertButton.setVisible(visible);
    }

    /**
     * Changes visibility of update button
     * 
     * @param visible set to updateButton visible flag
     */
    public void showUpdateButton(boolean visible) {
        updateButton.setVisible(visible);
    }

    /**
     * Changes visibility of delete button
     * 
     * @param visible set to deleteButton visible flag
     */
    public void showDeleteButton(boolean visible) {
        deleteButton.setVisible(visible);
    }

    /**
     * Adds selection listener to insert button.
     * 
     * @param listener set insertButton listener.
     */
    public void setInsertListener(SelectionListener<ButtonEvent> listener) {
        insertButton.addSelectionListener(listener);
    }

    /**
     * Adds selection listener to update button.
     * 
     * @param listener set updateButton listener.
     */
    public void setUpdateListener(SelectionListener<ButtonEvent> listener) {
        updateButton.addSelectionListener(listener);
    }

    /**
     * Adds selection listener to dlete button.
     * 
     * @param listener set deleteButton listener.
     */
    public void setDeleteListener(SelectionListener<ButtonEvent> listener) {
        deleteButton.addSelectionListener(listener);
    }

    /**
     * Adds selection listener to select button.
     * 
     * @param listener set selectButton listener.
     */
    public void setSelectListener(SelectionListener<ButtonEvent> listener) {
        selectButton.addSelectionListener(listener);
    }

}
